/*
 * @Author: your name
 * @Date: 2022-03-19 19:33:41
 * @LastEditTime: 2022-04-17 22:14:26
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \driving\src\article\article.controller.ts
 */
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Req, Put } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';
import { Roles, RolesGuard } from 'src/auth/role.guard';
import { ArticleService } from './article.service';
import { CreateArticleDto } from './dto/create-article.dto';
import { SelectArticleDto, SelectDto } from './dto/select-article.dto';
import { UpdateArticleDto } from './dto/update-article.dto';

@ApiTags('文章')
@Controller('article')
export class ArticleController {
  constructor(private readonly articleService: ArticleService) {}
  @ApiOperation({ summary: '创建文章' })
  @ApiBearerAuth()
  @Post('creatArticle')
  @Roles('admin', 'coach','trainees')
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  async create(@Body() post: CreateArticleDto, @Req() req) {
      return await this.articleService.create(req.user, post);
  }

  @ApiOperation({ summary: '分页获取文章' })
  @ApiBearerAuth()
  @Post('getArticle')
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  async getArticle(@Body() post:SelectArticleDto){
    return await this.articleService.getArticle(post)
  }

  @ApiOperation({ summary: '获取全部文章' })
  @ApiBearerAuth()
  @Post('getAllArticle')
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  async getAllArticle(@Body() post:SelectArticleDto){
    return await this.articleService.getAllArticle(post)
  }

  @ApiOperation({ summary: '获取文章详情' })
  @ApiBearerAuth()
  @ApiParam({name:'id',description:'文章id'})
  @Get('getArticleDeail:id')
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  async getArticleDeail(@Param('id') id){
    return await this.articleService.getArticleDeail(id)
  }

  @ApiOperation({ summary: '更改文章状态' })
  @ApiBearerAuth()
  @ApiParam({name:'id',description:'文章id'})
  @Get('updataStatus:id')
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  async updataStatus(@Param('id') id){
    return await this.articleService.updataStatus(id)
  }

  @ApiOperation({ summary: '取消文章发布' })
  @ApiBearerAuth()
  @ApiParam({name:'id',description:'文章id'})
  @Get('cancelStatus:id')
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  async cancelStatus(@Param('id') id){
    return await this.articleService.cancelStatus(id)
  }

  @ApiOperation({ summary: '点赞数加一' })
  @ApiBearerAuth()
  @ApiParam({name:'id',description:'文章id'})
  @Get('addNice:id')
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  async addNice(@Param('id') id){
    return await this.articleService.addNice(id)
  }

  @ApiOperation({ summary: '删除文章' })
  @ApiBearerAuth()
  @ApiParam({name:'id',description:'文章id'})
  @Get('deleteArticle:id')
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  async deleteArticle(@Param('id') id){
    return await this.articleService.deleteArticle(id)
  }

  @ApiOperation({ summary: '查看自己发布的文章' })
  @ApiBearerAuth()
  @Post('getAriticleByUserID')
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  async getByUserID(@Body() post:SelectDto){
    return await this.articleService.getByUserID(post)
  }
}
